package com.matt.repository.course;

import com.matt.domain.course.CoursePlanClasses;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * *******************************
 * Created by Ron.
 * form com.matt
 * on 2018/3/16
 * *******************************
 */
public interface CoursePlanClassesRepository extends JpaRepository<CoursePlanClasses, String>{
    List<CoursePlanClasses> findByCoursePlanTopicId(String coursePlanTopicId);

    List<CoursePlanClasses> findByCoursePlanTopicIdAndClassesId(String coursePlanTopicId, String classesId);

    @Modifying
    Long deleteByCoursePlanTopicId(String coursePlanTopicId);


    @Transactional
    @Modifying
    Long deleteByIdIn(List<String> ids);

    @Transactional
    @Modifying
    @Query("update CoursePlanClasses c set c.courseStatus = ?1 where c.id = ?2 ")
    int updateCourseStatusById(@Param("courseStatus") Integer courseStatus, @Param("classesCoursePlanId") String classesCoursePlanId);

    List<CoursePlanClasses> findByClassesIdAndCourseDateAndCategory(String classesId, Date courseDate, Integer category);

    List<CoursePlanClasses> findByAreaIdAndCategoryAndCourseDateBetween(String areaId, Integer category, Date begin, Date end);

    List<CoursePlanClasses> findByCourseDateBefore(Date courseDate);

    List<CoursePlanClasses> findByCourseDateBetween(Date begin, Date end);

    @Query(value="select cc.* from course_plan_classes cc,classes cs,course c where cs.id = cc.classesId and cc.courseId = c.id and DATE_FORMAT(DATE_SUB(cc.courseDate,INTERVAL 1 DAY),'%Y-%m-%d %H:%i')<now() and cc.prepareStatus=?1 order by cc.courseDate asc, cc.beginTime asc, cc.endTime asc",nativeQuery=true)
    List<CoursePlanClasses> findByPrepareStatus(@Param("prepareStatus") Integer prepareStatus);
    
    List<CoursePlanClasses> findByAreaIdAndCourseDateBetweenAndClassesId(String areaId, Date begin, Date end,String classesId);

}
